#!/bin/bash -ex
# SPDX-License-Identifier: LGPL-2.1+

PARAMS="--with=fuse"

CASYNC_PROTOCOL_PATH=@top_builddir@
export CASYNC_PROTOCOL_PATH

SCRATCH_DIR=${TMPDIR:-/var/tmp}/test-casync.$RANDOM
mkdir -p $SCRATCH_DIR/src

if [ $UID = 0 ]; then
    cp -a @top_srcdir@ $SCRATCH_DIR/src
else
    # If we lack privileges we use rsync rather than cp to copy, as it will just skip over device nodes
    rsync --exclude=.cacac -a @top_srcdir@ $SCRATCH_DIR/src
fi

@top_builddir@/casync $PARAMS digest $SCRATCH_DIR/src > $SCRATCH_DIR/test.digest

@top_builddir@/casync $PARAMS make $SCRATCH_DIR/test.caidx $SCRATCH_DIR/src
@top_builddir@/casync $PARAMS digest $SCRATCH_DIR/test.caidx > $SCRATCH_DIR/test.caidx.digest

diff -q $SCRATCH_DIR/test.digest $SCRATCH_DIR/test.caidx.digest

@top_builddir@/casync $PARAMS extract $SCRATCH_DIR/test.caidx $SCRATCH_DIR/extract
@top_builddir@/casync $PARAMS digest $SCRATCH_DIR/extract > $SCRATCH_DIR/extract.digest

diff -q $SCRATCH_DIR/test.digest $SCRATCH_DIR/extract.digest

if [ $UID = 0 ]; then
    modprobe fuse ||:
    if [ -e /dev/fuse ]; then
        MOUNT_PID=`@top_builddir@/notify-wait @top_builddir@/casync $PARAMS mount $SCRATCH_DIR/test.caidx $SCRATCH_DIR/mount`

        @top_builddir@/casync $PARAMS digest $SCRATCH_DIR/extract > $SCRATCH_DIR/mount.digest

        diff -q $SCRATCH_DIR/test.digest $SCRATCH_DIR/mount.digest

        kill $MOUNT_PID
        umount $SCRATCH_DIR/mount ||:
    fi
fi

rm -rf $SCRATCH_DIR
